﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace lcs.Web.Outstorage
{
    public partial class ListReturnGoods : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;
                    int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());
                    Bind(user_id);
                }

            }

        }

        private void Bind(int user_id)
        {
            string strWhere = " user_id=" + user_id;

            if (!string.IsNullOrEmpty(this.txt_projectname.Text))
            {
                strWhere += " and projectName like '%" + txt_projectname.Text+"%' ";
            }

            if (!string.IsNullOrEmpty(this.txt_material.Text))
            {
                strWhere += " and material like '%" + txt_material.Text + "%' ";
            }

            if (!string.IsNullOrEmpty(this.txt_returnNo.Text))
            {
                strWhere += " and returnNo like '%" + txt_returnNo.Text + "%' ";
            }
            if (!string.IsNullOrEmpty(this.txt_return_user_name.Text))
            {
                strWhere += " and return_user_name like '%" + txt_return_user_name.Text + "%' ";
            }
            if (!string.IsNullOrEmpty(this.txt_purposeProjectName.Text))
            {
                strWhere += " and purposeProjectName like '%" + txt_return_user_name.Text + "%' ";
            }
            
            DataSet ds = new lcs.BLL.returnGoods().GetList(strWhere);

            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
        }

        //查询
        protected void btn_sel_Click(object sender, EventArgs e)
        {
            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;
                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());
                Bind(user_id);
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "lbn_print")
            {
                string returnNo = e.CommandArgument.ToString();

                if (!string.IsNullOrEmpty(returnNo))
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>OpenPopWin('PrintReturnGoods.aspx?returnNo=" + returnNo + "','600px','800px');</script>");
                }
               
            }
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;

            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;
                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());
                Bind(user_id);
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                        e.Row.Cells[i].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                }
            }
            if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行
            {
                //当鼠标放上去的时候 先保存当前行的背景颜色 并设置新的背景色
                e.Row.Attributes.Add("onmouseover", "onmouseroverop(this)");
                //当鼠标离开的时候 将背景颜色恢复成之前的颜色
                e.Row.Attributes.Add("onmouseout", "onmouseroutop(this)");
                //单击行改变行背景颜色
                e.Row.Attributes.Add("onclick", "ItemOver(this)");
            }
        }

        //导出复写
        public override void VerifyRenderingInServerForm(Control control)       //导出
        { }

        protected void btn_out_Click(object sender, EventArgs e)
        {
            this.GridView1.AllowPaging = false;

            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;
                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());
                Bind(user_id);
            }

            this.GridView1.Columns[0].Visible = false;
            //this.GridView1.Columns[1].Visible = false;

            if (this.GridView1.Rows.Count != 0)
            {
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "GB2312";
                Response.AppendHeader("Content-Disposition", "attachment;filename=TuiKuQingDan.xls");
                //Response.ContentEncoding = System.Text.Encoding.UTF7;
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                Response.ContentType = "application/ms-excel";
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                Response.Output.Write("<H4 align='center'>退库清单</H4>");
                this.GridView1.RenderControl(oHtmlTextWriter);
                Response.Output.Write(oStringWriter.ToString());
                Response.Flush();
                Response.End();

            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('数据为空，无法导出表格')</script>");

            }
        }
    }
}